package com.mys.dxc;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.mysql.jdbc.Statement;

public final class mydatasource{
	
//	private static String url = "jdbc:mysql://localhost:3306/test";
//	private static String username = "root";
//	private static String password = "123456";
	private static DataSource myDataSource = null;
	private mydatasource(){
		
	}
	
	static {
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Properties prop = new Properties();
			InputStream is = mydatasource.class.getResourceAsStream("dbcpconfig.properties");
			prop.load(is);
			myDataSource = BasicDataSourceFactory.createDataSource(prop);
		}
		catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static DataSource getDataSource(){
		return myDataSource;
	}
	
	public static Connection getConnection() throws Exception{
		return myDataSource.getConnection();
	}
	
	public static void free(ResultSet rs,Statement st,Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			try{
				if(st!=null){
					st.close();
				}
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
			finally{
				if(conn!=null){
					try {
						conn.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			}
		}
	}
	
	
}